package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketFlowDao;
import com.bestcem.xm.ticket.dao.converter.TicketFlowConverter;
import com.bestcem.xm.ticket.dao.mapper.TicketFlowDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.TicketFlow;
import com.bestcem.xm.ticket.entity.mysql.TicketFlowDO;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 10:58
 */
@Repository
public class TicketFlowDaoImpl implements TicketFlowDao {
    @Resource
    private TicketFlowDaoMapper ticketFlowDaoMapper;

    @Override
    public TicketFlow findById(String id) {
        TicketFlowDO ticketFlowDO = ticketFlowDaoMapper.findByTicketFlowId(id);
        return TicketFlowConverter.doToDto(ticketFlowDO);
    }

    @Override
    public List<TicketFlow> findByConfigId(String ticketConfigId) {
        List<TicketFlowDO> ticketFlowDOList = ticketFlowDaoMapper.listByTicketConfigId(ticketConfigId);

        return TicketFlowConverter.doListToDtoList(ticketFlowDOList);
    }

    @Override
    public TicketFlow saveTicketFlow(TicketFlow ticketFlow) {
        TicketFlowDO entityDo = TicketFlowConverter.dtoToDo(ticketFlow);
        if (Objects.isNull(entityDo)) {
            entityDo = new TicketFlowDO();
        }

        if (ObjectUtils.isEmpty(entityDo.getTicketFlowId())) {
            entityDo.setTicketFlowId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getFlowId())) {
            entityDo.setFlowId(0);
        }
        if (Objects.isNull(entityDo.getStatus())) {
            entityDo.setStatus(0);
        }
        if (Objects.isNull(entityDo.getMatchCondition())) {
            entityDo.setMatchCondition(0);
        }
        if (Objects.isNull(entityDo.getPriority())) {
            entityDo.setPriority(0);
        }
        if (Objects.isNull(entityDo.getNeedAudit())) {
            entityDo.setNeedAudit(false);
        }
        if (Objects.isNull(entityDo.getVersion())) {
            entityDo.setVersion(0);
        }

        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }
        ticketFlowDaoMapper.insert(entityDo);

        return TicketFlowConverter.doToDto(entityDo);
    }

    @Override
    public List<TicketFlow> findTicketFlowByStatus(String ticketConfigId, int status) {
        List<TicketFlowDO> ticketFlowDOList = ticketFlowDaoMapper.listByTicketConfigIdAndStatus(ticketConfigId, status);

        return TicketFlowConverter.doListToDtoList(ticketFlowDOList);
    }

    @Override
    public List<TicketFlow> findTicketFlowById(String ticketConfigId, List<String> ticketFlowIds) {
        List<TicketFlowDO> ticketFlowDOList = ticketFlowDaoMapper
                .listByTicketConfigIdAndTicketFlowIdList(ticketConfigId, ticketFlowIds);

        return TicketFlowConverter.doListToDtoList(ticketFlowDOList);
    }

    @Override
    public TicketFlow updateTicketFlow(String id, TicketFlow ticketFlow) {
        if (Objects.isNull(ticketFlow)) {
            return null;
        }
        TicketFlowDO entityDo = TicketFlowConverter.dtoToDo(ticketFlow);
        if (Objects.isNull(entityDo)) {
            return null;
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        entityDo.setUpdateTime(DateUtil.getCommonDate());
        ticketFlowDaoMapper.updateById(entityDo);

        return TicketFlowConverter.doToDto(entityDo);
    }
}
